<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>WebGL 2.0 Samples</title>
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>

            @font-face {
                font-family: 'inconsolata';
                src: url('assets/fonts/inconsolata.woff') format('woff');
                font-weight: normal;
                font-style: normal;
            }

            * {
                box-sizing: border-box;
            }

            html {
                height: 100%;
            }

            body {
                background-color: #ffffff;
                margin: 0px;
                height: 100%;
                color: #555;
                font-family: 'inconsolata';
                font-size: 15px;
                line-height: 18px;
                overflow: hidden;
            }

            h1 {
                margin-top: 30px;
                margin-bottom: 10px;
                margin-left: 20px;
                font-size: 25px;
                font-weight: normal;
            }

            h2 {
                font-size: 20px;
                font-weight: normal;
            }

            a {
                color: #2194CE;
                text-decoration: none;
            }

            #githubLink {
                margin-left: 20px;
                margin-bottom: 30px;
            }

            #panel {
                position: fixed;
                left: 0px;
                width: 310px;
                height: 100%;
                overflow: auto;
                background: #fafafa;
            }

                #panel #content {
                    padding: 0px 20px;
                }

                #panel #content .link {
                    color: #2194CE;
                    text-decoration: none;
                    cursor: pointer;
                }

                #panel #content .selected {
                    color: #ff0000;
                }

                    #panel #content .link:hover {
                        text-decoration: underline;
                    }

            #viewer {
                position: absolute;
                border: 0px;
                left: 310px;
                width: calc(100% - 310px);
                height: 100%;
                overflow: auto;
            }

            #button {
                position: fixed;
                bottom: 20px;
                right: 20px;
                padding: 8px;
                color: #fff;
                background-color: #555;
                opacity: 0.7;
            }

                #button:hover {
                    cursor: pointer;
                    opacity: 1;
                }

            /* mobile */

            #expandButton {
                display: none;
                position: absolute;
                right: 20px;
                top: 12px;
                width: 32px;
                height: 32px;
            }

                #expandButton span {
                    height: 2px;
                    background-color: #2194CE;
                    width: 16px;
                    position: absolute;
                    left: 8px;
                    top: 10px;
                }

                #expandButton span:nth-child(1) {
                    top: 16px;
                }

                #expandButton span:nth-child(2) {
                    top: 22px;
                }

            @media all and ( max-width: 640px ) {
                h1{
                    margin-top: 20px;
                    margin-bottom: 20px;
                }
                #panel{
                    position: absolute;
                    left: 0;
                    top: 0;
                    height: 480px;
                    width: 100%;
                    right: 0;
                    z-index: 100;
                    overflow: hidden;
                    border-bottom: 1px solid #dedede;
                }
                #content{
                    position: absolute;
                    left: 0;
                    top: 60px;
                    right: 0;
                    bottom: 0;
                    font-size: 17px;
                    line-height: 22px;
                    overflow: auto;
                }
                #viewer{
                    position: absolute;
                    left: 0;
                    top: 56px;
                    width: 100%;
                    height: calc(100% - 56px);
                }
                #expandButton{
                    display: block;
                }
                #panel.collapsed{
                    height: 56px;
                }
            }

        </style>
    </head>
    <body>
        <div id="panel" class="collapsed">
            <h1>WebGL 2.0 Samples</h1>
            <div id="githubLink">(<a href="https://github.com/WebGLSamples/WebGL2Samples">GitHub repo</a>)</div>
            <a id="expandButton" href="#">
                <span></span>
                <span></span>
                <span></span>
            </a>
            <div id="content"></div>
        </div>
        <iframe id="viewer" allowfullscreen onmousewheel=""></iframe>

        <script>

        var files = {
            "Draw": [
                "draw_image_space",
                "draw_instanced",
                "draw_primitive_restart",
                "draw_range_arrays"
            ],
            "Frame Buffer Object": [
                "fbo_blit",
                "fbo_multisample",
                "fbo_new_blend_equation",
                "fbo_rtt_draw_buffers",
                "fbo_rtt_texture_array",
                "fbo_rtt_depth_texture",
                "fbo_read_pixels"
            ],
            "Sampler": [
                "sampler_object",
                "sampler_filter",
                "sampler_wrap"
            ],
            "GLSL": [
                "glsl_centroid",
                "glsl_flat_smooth_interpolators",
                "glsl_non_square_matrix"
            ],
            "Query": [
                "query_occlusion"
            ],
            "Buffer":[
                "draw_instanced_ubo",
                "buffer_copy",
                "buffer_uniform",
            ],
            "Texture": [
                "texture_2d_array",
                "texture_3d",
                "texture_derivative",
                "texture_fetch",
                "texture_format",
                "texture_grad",
                "texture_immutable",
                "texture_integer",
                "texture_lod",
                "texture_offset",
                "texture_pixel_store",
                "texture_srgb",
                "texture_vertex"
            ],
            "Transform Feedback": [
                "transform_feedback_interleaved",
                "transform_feedback_separated",
                "transform_feedback_separated_2",
                "transform_feedback_instanced"
            ],
            "Geometry": [
                "geo_vertex_format"
            ]
        };

        var panel = document.getElementById( 'panel' );
        var content = document.getElementById( 'content' );
        var viewer = document.getElementById( 'viewer' );

        var expandButton = document.getElementById( 'expandButton' );
        expandButton.addEventListener( 'click', function ( event ) {
            panel.classList.toggle( 'collapsed' );
            event.preventDefault();
        } );

        // iOS8 workaround

        if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {

            viewer.addEventListener( 'load', function ( event ) {

                viewer.contentWindow.innerWidth -= 10;
                viewer.contentWindow.innerHeight -= 2;

            } );

        }

        var container = document.createElement( 'div' );
        content.appendChild( container );

        var button = document.createElement( 'div' );
        button.id = 'button';
        button.textContent = 'View source';
        button.addEventListener( 'click', function ( event ) {

            var array = location.href.split( '/' );
            array.pop();

            var sampleDoc = viewer.contentDocument || viewer.contentWindow.document;
            var highlightedLines = sampleDoc.getElementById('highlightedLines') || '';
            var lines = !!highlightedLines ? highlightedLines.innerHTML : '';
            window.open( 'https://github.com/WebGLSamples/WebGL2Samples/blob/master/samples/' + selected + '.html' + lines );

        }, false );
        button.style.display = 'none';
        document.body.appendChild( button );

        var divs = {};
        var selected = null;

        for ( var key in files ) {

            var section = files[ key ];

            var div = document.createElement( 'h2' );
            div.textContent = key;
            container.appendChild( div );

            for ( var i = 0; i < section.length; i ++ ) {
                /*jshint loopfunc: true */
                ( function ( file ) {

                    var div = document.createElement( 'div' );
                    div.className = 'link';
                    div.textContent = file;
                    div.addEventListener( 'click', function () {

                        panel.classList.toggle( 'collapsed' );
                        load( file );

                    } );
                    container.appendChild( div );

                    divs[ file ] = div;

                } )( section[ i ] );



            }

        }

        var load = function ( file ) {

            if ( selected !== null ) divs[ selected ].className = 'link';

            divs[ file ].className = 'link selected';

            window.location.hash = file;
            viewer.src = 'samples/' + file + '.html';
            viewer.focus();

            button.style.display = '';

            selected = file;

        };

        if ( window.location.hash !== '' ) {

            load( window.location.hash.substring( 1 ) );

        }

        </script>

    </body>
</html>
